﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Net.Cache;
using System.Net;

namespace Transcend.DP3.Http
{
	public class WebRequestConfigurator
	{

		private NameValueCollection dataFields = new NameValueCollection();
		/// <summary>
		/// 取已添加的数据域
		/// </summary>
		public NameValueCollection DataFields
		{
			get { return dataFields; }
			set { dataFields = value; }
		}

		private NameValueCollection fileFields = new NameValueCollection();
		/// <summary>
		/// 取已经添加的文件域
		/// </summary>
		public NameValueCollection FileFields
		{
			get { return fileFields; }
			set { fileFields = value; }
		}

		private WebHeaderCollection header = new WebHeaderCollection();
		/// <summary>
		/// 设置/获取http头信息
		/// </summary>
		public WebHeaderCollection Headers
		{
			get { return header; }
			set { header = value; }
		}

		private Encoding requestEncoding = Encoding.UTF8;
		/// <summary>
		/// http字符编码方式，默认UTF8
		/// </summary>
		public Encoding RequestEncoding
		{
			get { return requestEncoding; }
			set { requestEncoding = value; }
		}

		private int requestTimeout = 1000;
		/// <summary>
		/// 默认请求超时时间
		/// </summary>
		public int RequestTimeout
		{
			get { return requestTimeout; }
			set { requestTimeout = value; }
		}

		private int readWriteTimeout = 300000;
		/// <summary>
		/// 读写数据超时时间(ms)，默认300000ms
		/// </summary>
		public int ReadWriteTimeout
		{
			get { return readWriteTimeout; }
			set { readWriteTimeout = value; }
		}

		private int sndBufferSize = 2048;
		/// <summary>
		/// 发送数据缓冲区大小，默认 1024 byte
		/// </summary>
		public int SndBufferSize
		{
			get { return sndBufferSize; }
			set { sndBufferSize = value; }
		}
		
		private int rcvBufferSize = 20480;
		/// <summary>
		/// 接收数据缓冲区大小，默认 2048 byte
		/// </summary>
		public int RcvBufferSize
		{
			get { return rcvBufferSize; }
			set { rcvBufferSize = value; }
		}

		private string userAgent = string.Empty;
		/// <summary>
		/// 客户端
		/// </summary>
		public string UserAgent
		{
			get { return userAgent; }
			set { userAgent = value; }
		}

		private string accept = string.Empty;
		/// <summary>
		/// http accept头信息
		/// </summary>
		public string Accept
		{
			get { return accept; }
			set { accept = value; }
		}

		private string referer = string.Empty;
		/// <summary>
		/// 引用页
		/// </summary>
		public string Referer
		{
			get { return referer; }
			set { referer = value; }
		}

		private bool keepAlive = true;
		/// <summary>
		/// 是否保持连接，详见HttpWebRequest.KeepAlive
		/// </summary>
		public bool KeepAlive
		{
			get { return keepAlive; }
			set { keepAlive = value; }
		}

		private string expect = string.Empty;
		/// <summary>
		/// http Expect头信息
		/// </summary>
		public string Expect
		{
			get { return expect; }
			set { expect = value; }
		}
		
		private HttpRequestCachePolicy _requestCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
		/// <summary>
		/// 请求换缓存策略，默认无缓存
		/// </summary>
		public HttpRequestCachePolicy RequestCachePolicy
		{
			get { return _requestCachePolicy; }
			set { _requestCachePolicy = value; }
		}

		private Version _protocolVersion = HttpVersion.Version11;
		/// <summary>
		/// Http协议版本，默认 http协议1.1
		/// </summary>
		public Version ProtocolVersion
		{
			get { return _protocolVersion; }
			set { _protocolVersion = value; }
		}
		
		private bool _enableExpect100Continue = false;
		/// <summary>
		/// 接收到Server返回的100-continue应答以后, 才把数据POST给Server，默认false。设置该项可能将增加响应时间
		/// </summary>
		public bool EnableExpect100Continue
		{
			get { return _enableExpect100Continue; }
			set { _enableExpect100Continue = value; }
		}

		private CookieContainer ckContainer = new CookieContainer();
		/// <summary>
		/// cookie容器
		/// </summary>
		public CookieContainer CkContainer
		{
			get { return ckContainer; }
			set { ckContainer = value; }
		}

		/// <summary>
		/// 添加要上传文件
		/// </summary>
		/// <param name="name">域名称</param>
		/// <param name="path">文件路径，仅支持本地文件</param>
		public void AddFile(string name, string path)
		{
			fileFields.Add(name, path);
		}

		public void AddFile(NameValueCollection c)
		{
			fileFields.Add(c);
		}

		/// <summary>
		/// 添加域值对
		/// </summary>
		/// <param name="name">域名称</param>
		/// <param name="value">域值</param>
		public void AddField(string name, string value)
		{
			dataFields.Add(name, value);
		}

		public void AddField(NameValueCollection c)
		{
			dataFields.Add(c);
		}

		/// <summary>
		/// 添加http头信息
		/// </summary>
		/// <param name="name">头域名称</param>
		/// <param name="value">头域值</param>
		public void AddHeader(string name, string value)
		{
			header.Add(name, value);
		}

		/// <summary>
		/// 创建默认的IE配置信息，主要是模拟IE特定的HTTP头信息
		/// </summary>
		/// <returns></returns>
		public static WebRequestConfigurator CreateIEConfigurator()
		{
			WebRequestConfigurator cfg = new WebRequestConfigurator();
			cfg.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
			cfg.Accept = "text/html, application/xhtml+xml, */*";
			cfg.Headers.Add("Accept-Encoding", "gzip, deflate");
			cfg.Headers.Add("Accept-Language", "zh-CN");
			//cfg.Headers.Add("Connection", "close");
			return cfg;
		}

		/// <summary>
		/// 创建默认的chrome配置信息，主要是模拟Chrome特定的HTTP头信息
		/// </summary>
		/// <returns></returns>
		public static WebRequestConfigurator CreateChromeConfigurator()
		{
			WebRequestConfigurator cfg = new WebRequestConfigurator();
			cfg.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36";
			cfg.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
			//cfg.Headers.Add("Connection", "close");
			cfg.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
			cfg.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6");
			return cfg;
		}
	}

}
